package com.huawei.hmsdemo10086.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.huawei.hmsdemo10086.POJO.Address;
import com.huawei.hmsdemo10086.POJO.Goods;
import com.huawei.hmsdemo10086.POJO.Result;
import com.huawei.hmsdemo10086.R;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Order_DetailActivity extends AppCompatActivity {

    Intent intent;
    private TextView address;
    private TextView name;
    private TextView phone;
    private TextView mTitleTextView;
    private Button back;
    private TextView region;
    private TextView price;
    private TextView title;
    private TextView num;
    private Button delete;
    private ImageView img;
    private TextView order_price;
    private TextView order_num;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        mTitleTextView = (TextView) findViewById(R.id.text_title);
        mTitleTextView.setText("订单详情");
        back = (Button)findViewById(R.id.button_backward);
        intent = this.getIntent();
        title = (TextView) findViewById(R.id.goods_title);
        num = (TextView) findViewById(R.id.goods_num);
        price = (TextView) findViewById(R.id.goods_price);
        name = (TextView) findViewById(R.id.address_name);
        address = (TextView) findViewById(R.id.input_address);
        phone = (TextView) findViewById(R.id.address_phone);
        delete = (Button)findViewById(R.id.delete_order) ;
        region = (TextView)findViewById(R.id.address_region);
        img = findViewById(R.id.car_img);
        order_num = findViewById(R.id.order_num);
        order_price = findViewById(R.id.order_price);

        order_num.setText("数量："+intent.getStringExtra("goods_num")+"件");
        order_price.setText("总价：¥"+intent.getIntExtra("goods_num",0)*intent.getIntExtra("goods_price",0));
        title.setText(intent.getStringExtra("goods_name"));
        num.setText(intent.getStringExtra("goods_num"));
        price.setText(intent.getStringExtra("goods_price"));

        //Glide设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(10);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        // RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(20, 20);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);

        Glide.with(this).load(intent.getStringExtra("goods_img_url"))
                .apply(options)
                .into(img);

        System.out.println(intent.getStringExtra("goods_name"));
        System.out.println(intent.getStringExtra("goods_num"));
        System.out.println(intent.getStringExtra("goods_price"));
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Order_DetailActivity.this,OrderActivity.class);
                startActivity(intent);
            }
        });
        getAddress();

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteOrder();
            }
        });

    }


    private void getAddress()
    {
        String url = "http://10.0.2.2:8443/api/findAddressById";
        OkHttpClient mOkHttpClient = new OkHttpClient();

        String json = "{\n" +
                "  \"addressid\": \"" + Integer.parseInt(intent.getStringExtra("address_id")) + "\"\n" +
                "}";


        Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                Gson gson = new Gson();
                String rep = response.body().string();
                System.out.println(rep);
                Type type = new TypeToken<Result<Address>>(){}.getType();
                Result<Address> result = gson.fromJson(rep,type);
                System.out.println(result.getData().toString());
                name.setText(result.getData().getName());
                address.setText(result.getData().getAddress());
                phone.setText(result.getData().getPhone());
                region.setText(result.getData().getRegion());
                System.out.println(result.getData().getName());
                System.out.println(result.getData().getAddress());
                System.out.println(result.getData().getPhone());
            }
        });
    }

    private void deleteOrder()
    {
        String url = "http://10.0.2.2:8443/api/deleteOrder";
        OkHttpClient mOkHttpClient = new OkHttpClient();

        String json = "{\n" +
                "  \"goodsid\": \"" +Integer.parseInt(intent.getStringExtra("goods_id"))+ "\",\n" +
                "  \"orderid\": \"" + Integer.parseInt(intent.getStringExtra("order_id")) + "\"\n" +
                "}";


        Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Intent intent_jump = new Intent(Order_DetailActivity.this,OrderActivity.class);
                startActivity(intent_jump);
            }
        });
    }


}